package fishBids.group3.behaviour.bidder;

import jade.content.Concept;
import jade.content.ContentElement;
import jade.content.lang.Codec;
import jade.content.onto.Ontology;
import jade.content.onto.basic.Action;
import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.SimpleBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import es.upm.agents.ontology.Product;
import es.upm.agents.ontology.ToSellProduct;
import es.upm.agents.ontology.ToSellProductResponse;
import fishBids.group3.common.Constants;
import fishBids.group3.utils.Utils;

public class SellProductBidderBehaviour extends SimpleBehaviour {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7518636912428557333L;
	
	public SellProductBidderBehaviour(Codec codec, Ontology ontology, AID bankID, Agent creatorAgent, Product product ){
		this.codec = codec;
		this.ontology = ontology;
		this.bankID = bankID;
		this.creatorAgent = creatorAgent;
		this.product = product;
		
	}
	
	boolean end = false;

	public void action()

	{	
		if(status == 0){
			
			ToSellProduct toSellProduct = new ToSellProduct();
			toSellProduct.setAmount(product.getAmount());
			toSellProduct.setProductType(product.getProductType());
			
			Utils.sendMessageWithConcept(ACLMessage.REQUEST, codec, ontology, bankID, Constants.SELL_PRODUCT, myAgent,toSellProduct);
//			System.out.println("Bidder>> SellProduct request sent, Waiting for an AGREE message");
			status = 1;
		}
		
		// Waits for the AGREE of an answer
		// Receive the message 
		ACLMessage msg = creatorAgent.receive(MessageTemplate.MatchProtocol(Constants.SELL_PRODUCT) ); 
		
		if( msg != null ){
		 
		  ContentElement ce = null; 
		  if (msg.getPerformative() == ACLMessage.AGREE) { 
			//System.out.println("Bidder>> AGREE message arrived");
			// Waits for the arrival of INFORM
			//System.out.println("Bidder>> Waiting for an INFORM or FAILURE message from Bank, SellProduct protocol");
			status = 2;
			// Receive the message 
			return;
		  }
			try { 
				  if (msg.getPerformative() == ACLMessage.INFORM) { 
					  //System.out.println("Bidder>> SellProduct INFORM message arrived from Bank");
					  ce = creatorAgent.getContentManager().extractContent(msg);
					  if( ce instanceof Action ){
						  Action ac = (Action)ce;
						  Concept cpt = ac.getAction();
						  if( cpt instanceof ToSellProductResponse){
							  ToSellProductResponse toSellProductResponse = (ToSellProductResponse) cpt;
							  System.out.println("bidder3: sells " + product.getAmount() + " kg. of " + product.getProductType().getType() + " by "  + toSellProductResponse.getRevenue() + " euros" );
							  end = true;
						  }
					  }
					  return;	
				  }
				  if (msg.getPerformative() == ACLMessage.FAILURE) { 
//					  System.out.println("Bidder>>> SellProduct FAILURE message arrived");
					  end = true;
					  return;
				  }
			
			
				}catch (Exception e) {
					e.printStackTrace();
				}
		  if (msg.getPerformative() == ACLMessage.NOT_UNDERSTOOD) { 
//			  System.out.println(creatorAgent.getName() + " got a NOT_UNDERSTOOD in SellProduct, check!!!!! :(");
			  end=true;
			  return;
		  }
		  
		}else{
			
			block();
			
		}
	}



	public boolean done()

	{

		return end;

	}
	
	Codec codec;
	Ontology ontology;
	AID bankID;
	Agent creatorAgent;
	int status = 0;
	Product product = new Product();

}
